import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/manage/Login'
import Register from '@/components/manage/Register'
import Homepage from '@/components/Homepage'
import Customer from '@/components/Customer'
import Driver from '@/components/Driver'
import Vehicle from '@/components/Vehicle'
import Userinfo from '@/components/Userinfo'
import Business from "../components/Business";

Vue.use(Router)

const router =  new Router({

  routes: [

    {
      path:'/login',
      name:'login',
      component: Login
    },
    {
      path:'/',
      name:'login',
      component: Login
    },
    {
      path:'/register',
      name:'register',
      component: Register

    },
    {
      path:'/homepage',
      name:'homepage',
      component:Homepage,
      children:[

        {
          path:'/homepage/customer',
          name:'customer',
          component:Customer
        },
        {
          path:'/homepage/driver',
          name:'driver',
          component:Driver
        },
        {
          path:'/homepage/vehicle',
          name:'vehicle',
          component:Vehicle
        },
        {
          path:'/homepage/userinfo',
          name:'userinfo',
          component:Userinfo
        },
        {
          path:'/homepage/business',
          name:'business',
          component:Business
        }
      ]
    }
  ],
  mode:'history'
})

//路由导航守卫
router.beforeEach((to,from,next)=>{

  if (to.path =='/login'||to.path=='/register') return next();
  const tokenStr = sessionStorage.getItem('token');
  console.log(sessionStorage);
  if (!tokenStr) {
    return next('/login');
  }
  const isPermission = sessionStorage.getItem('permission');
  if (to.path=='/homepage/userinfo'&&isPermission==0){
    alert('无权限');

    return next('/homepage/vehicle');

  }else{

  }
  return  next();
})

export default router
